package dao.impl;

import dao.FormeDao;
import dao.dto.FormeDto;
import boerrors.FormeInexistantException;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;


public class FormeDaoImpl extends FormeDao {
        private static FormeDao _instance = null;
        private CallableStatement _callableStatement = null;
        private SQLConnection _connection = new SQLConnection();
        
        //Constructeur du singleton
	public static FormeDao getInstance(){
		if(_instance == null)
			_instance = new FormeDaoImpl();
		return _instance;
	}
        
        //Methodes
        @Override
        public Vector<FormeDto> lister() throws FormeInexistantException {
                ResultSet resultat = null;
                FormeDto forme = null;
                try {                                      
                        //query
                        _callableStatement = _connection.getConnection().prepareCall("{call sp_FormeList}");
                        //execute
                        resultat = _callableStatement.executeQuery();
                        Vector<FormeDto> liste = new Vector<FormeDto>();
                        while (resultat.next()) {
                                forme = new FormeDto(resultat.getInt("idForme"),resultat.getString("libForme"));         
                                liste.add(forme);
                        }
                        return liste;
                } catch(SQLException e) { 
                        throw new FormeInexistantException(e);
                } finally { 
                    if (resultat != null) try { resultat.close(); } catch(Exception e) { throw new FormeInexistantException(e);}
                    if (_callableStatement != null) try { _callableStatement.close(); } catch(Exception e) {throw new FormeInexistantException(e);}
                    _connection.deconnecter();
                }
        }
        
        @Override
        public FormeDto selectId(int idForme) throws FormeInexistantException {
                ResultSet resultat = null;
                FormeDto forme = null;
                try {                                      
                        //query
                        _callableStatement = _connection.getConnection().prepareCall("{call sp_FormeSelectId}");
                        //parameter
                        _callableStatement.setInt("idForme",idForme);
                        //execute
                        resultat = _callableStatement.executeQuery();
                        if (resultat.next()) {
                                forme = new FormeDto(resultat.getInt("idForme"),resultat.getString("libForme"));         
                        }
                        return forme;
                } catch(SQLException e) { 
                        throw new FormeInexistantException(e);
                } finally { 
                    if (resultat != null) try { resultat.close(); } catch(Exception e) { throw new FormeInexistantException(e);}
                    if (_callableStatement != null) try { _callableStatement.close(); } catch(Exception e) {throw new FormeInexistantException(e);}
                    _connection.deconnecter();
                }
        }
        
}